package logic

import (
	"context"

	"github.com/eeppee_admin/clouddisk/service/internal/svc"
	"github.com/eeppee_admin/clouddisk/service/internal/types"
	"github.com/eeppee_admin/clouddisk/service/model"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserFileMoveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserFileMoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserFileMoveLogic {
	return &UserFileMoveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserFileMoveLogic) UserFileMove(req *types.UserFileMoveRequest, userIdentity string) (resp *types.UserFileMoveResponse, err error) {

	parentData := new(model.UserStore)
	has, err := l.svcCtx.Engine.
		Where("identity = ? and user_identity = ?", req.ParentIdentity, userIdentity).
		Get(parentData)
	if err != nil {
		return nil, err
	}

	if !has {
		return nil, errors.New("文件夹不存在")
	}

	// 更新记录的parentId
	_, err = l.svcCtx.Engine.
		Where("identity = ?", req.Identity).
		Update(model.UserStore {
			ParentId: int64(parentData.Id),
		})

	return
}
